package Enum;

import java.sql.SQLOutput;

//枚举的构造方法默认是私有的
public enum TestEnum {
    //枚举对象
    RED(0.1,"红色"),BLACK(1.1,"黑色"),
    GREEN(2.1,"绿色"),WHITE(3.1,"白色");

    public String color;
    public double ordinal;

    @Override
    public String toString() {
        return "TestEnum{" +
                "color='" + color + '\'' +
                ", ordinal=" + ordinal +
                '}';
    }

    //枚举的构造方法默认是私有的（其他都不能写）
    private TestEnum(double ordinal,String color) {
        this.color = color;
        this.ordinal = ordinal;
    }

    public static void main(String[] args) {
        //values()：以数组形式返回枚举的所有成员
        TestEnum[] testEnums = TestEnum.values();

        for(int i = 0; i < testEnums.length; i++) {
            //ordinal()：此时会把枚举对象对应的索引号打印出来
            System.out.print(testEnums[i] + " " + testEnums[i].ordinal()+ " ");
        }
        System.out.println();
        //valueOf():将普通字符串转换成枚举实例
        TestEnum testEnum1 = TestEnum.valueOf("WHITE");
        System.out.println(testEnum1);//WHITE

        //compareTo()比较两个枚举变量定义时的顺序
        System.out.println(BLACK.compareTo(GREEN));
    }

    public static void main1(String[] args) {
        TestEnum testEnum = TestEnum.BLACK;
        switch(testEnum) {
            case RED:
                System.out.println("RED");
                break;
            case BLACK:
                System.out.println("BLACK");
                break;
            case GREEN:
                System.out.println("GREEN");
                break;
            default:
                System.out.println("颜色错误");
                break;
        }
    }
}
